Ext.define("core.product.controller.ProductController", {
    extend: "Ext.app.Controller",
    init: function () {
        var self = this;
        this.control({

            /**productgrid的添加按钮事件*/
            "product_grid button[ref='add']": {
                click: function (_btn) {
                    var win = Ext.widget("productWindow");
                    var form = win.down("form");
                    Ext.getCmp('quartzId').setReadOnly(false);
                    form.getForm().reset();
                    var btn = form.down("button[ref=reset]");
                    btn.enable(true);
                    // form.show();
                    // grid.hide();
                    // var grid = _btn.up("productlayout").down("product_grid");
                    // var form = grid.up("productlayout").down("productform");
                    win.show();
                }
            },
            /**productgrid的刷新按钮事件*/
            "product_grid button[ref='ref']": {
                click: function (_btn) {
                    var grid = _btn.up("productlayout").down("product_grid");
                    var store = grid.getStore();
                    store.load();
                }
            },
            /**productgrid的查找按钮事件*/
            "product_grid button[ref='searchBtn']": {
                click: function (_btn) {
                    var grid = _btn.up("product_grid");
                    var store = grid.getStore();
                    var gIdText = Ext.getCmp('gIdText').getValue();
                    var statText = Ext.getCmp('statText').getValue();
                    // Ext.Ajax.request({
                    //     waitMsg: '正在进行查询,请稍后...',
                    //     url: "batchSchedule/query.do",
                    //     params: {
                    //         pageNum:store.currentPage,
                    //         pageSize:store.pageSize,
                    //         groupId: gIdText,
                    //         status: statText
                    //     },// 根据id删除
                    //     method: "GET",
                    //     timeout: 4000,
                    //     success: function (response, opts) {
                    //         var resObj = Ext.decode(response.responseText);
                    //         if (resObj.success) {
                    //             store.load();
                    //         }
                    //     }
                    // });
                    //临时设置请求地址
                    store.proxy.url = "batchSchedule/query.do";
                    store.load({
                        params: {
                                    pageNum:store.currentPage,
                                    pageSize:store.pageSize,
                                    groupId: gIdText,
                                    status: statText
                                }
                    });
                    //恢复默认请求地址
                    store.proxy.url = "batchSchedule/listPage.do";
                }
            },
            /**productgrid的查找按钮事件*/
            "product_grid button[ref='resetBtn']": {
                click: function () {
                    Ext.getCmp('gIdText').setValue("");
                    Ext.getCmp('statText').setValue("");
                }
            },
            /**productgrid的添加按钮事件*/
            "product_grid button[ref='del']": {
                click: function (btn) {
                    var grid = btn.up("product_grid");
                    var store = grid.getStore();
                    //你选择的将要删除的记录
                    var records = grid.getSelectionModel().getSelection();
                    if (!records || records.length <= 0) {
                        Ext.Msg.alert("提示", "请选择需要删除的数据!");
                        return;
                    }
                    // 根据id删除多条记录
                    var data = [];
                    Ext.Array.each(records, function (model) {
                        data.push(Ext.JSON.encode(model.get('id')));
                    });
                    var ids = data.join(",");
                    Ext.MessageBox.confirm("请确认"
                        , "确定删除[<font color=red>" + ids + "</font>]吗？",
                        function (e) {
                            if (e == 'yes') {
                                Ext.Ajax.request({
                                    waitMsg: '正在进行处理,请稍后...',
                                    url: "batchSchedule/deleteSchedule.do",
                                    params: {
                                        ids: ids
                                    },// 根据id删除
                                    method: "POST",
                                    timeout: 4000,
                                    success: function (response, opts) {
                                        var resObj = Ext.decode(response.responseText);
                                        if (resObj.success) {
                                            // 不用查询，从grid中去掉对应的记录就OK了
                                            store.load({
                                                start:store.start,
                                                limit:store.limit
                                            });
                                            Ext.Msg.alert("提示", resObj.msg);
                                        } else {
                                            Ext.Msg.alert("提示", resObj.msg);
                                        }
                                    }
                                });
                            }
                        });


                }
            },
            // 连接测试功能
            "product_grid button[id='cmdBtn']": {
                click: function (btn) {
                    var grid = btn.up("product_grid");
                    var store = grid.getStore();
                    //你选择的将要删除的记录
                    var record = grid.getSelectionModel().getSelection();
                    if (!record || record.length != 1) {
                        return;
                    }
                    Ext.Msg.alert("提示", record[0].get("id"));
                    Ext.Ajax.request({
                        waitMsg: '正在进行处理,请稍后...',
                        url: "/generator-master/dataSource/testConnect.do",
                        params: {
                            id: record[0].get("id")
                        },// 根据id删除
                        method: "POST",
                        timeout: 4000,
                        success: function (response, opts) {
                            var resObj = Ext.decode(response.responseText);
                            if (resObj.success) {
                                Ext.Msg.alert("提示", resObj.msg);
                            } else {
                                Ext.Msg.alert("提示", resObj.msg);
                            }
                        }
                    });


                }
            },
            /**productgrid双击事件响应*/
            "product_grid": {
                itemdblclick: function (_grid, record, item, index, e, eOpts) {
                    var win = Ext.widget("productWindow");
                    var form = win.down("form");
                    // var form = _grid.up("productlayout").down("productform");
                    // var grid = _grid.up("productlayout").down("product_grid");
                    // //把选择的数据加载到form中去
                    // var _record = grid.getSelectionModel().getSelection();
                    var stTemp = record.get("startTime");
                    var spTemp = record.get("stopTime");
                    if (!(null == stTemp || Ext.util.Format.trim(stTemp) == "")) {
                        stTemp = stTemp.substring(0, 19);
                    }
                    if (!(null == spTemp || Ext.util.Format.trim(spTemp) == "")) {
                        spTemp = spTemp.substring(0, 19);
                    }
                    var format = 'Y-m-d H:i:s';
                    var stTime = Ext.Date.parse(stTemp, format, true);
                    var spTime = Ext.Date.parse(spTemp, format, true);
                    Ext.getCmp('quartzId').setReadOnly(true);
                    form.loadRecord(record);
                    form.getForm().findField("startTime").setValue(stTime);
                    form.getForm().findField("stopTime").setValue(spTime);
                    var btn = form.down("button[ref=reset]");
                    btn.disable(true);
                    // grid.hide();
                    // form.show();
                    // form.loadRecord(record);
                    win.show();
                }
            },

            /**===============================================*/
            "productWindow button[ref=return]": {
                click: function (btn) {
                    // var win = btn.ownerCt.ownerCt.ownerCt;
                    var win = Ext.getCmp("productWindow");
                    // var form = win.down("form");
                    // form.hide();
                    // var form = btn.up("productform");
                    // var grid = form.up("productlayout").down("product_grid");
                    // form.getForm().reset();
                    // form.hide();
                    // grid.show();
                    win.close();
                }
            },
            "product_grid combobox[id=prdPageSize]": {
                change: function (_this, newValue, oldValue, eOpts) {
                    var store = _this.up("product_grid").getStore();
                    store.pageSize = newValue;//设值新分页大小
                    store.currentPage = 1;//每次都从第一页开始加载
                    store.load();//用来加载数据
                }

            },
            //重置事件
            "productWindow button[ref=reset]": {
                click: function (btn) {
                    // var form = btn.up("productform");
                    var form = btn.ownerCt.ownerCt;
                    form.getForm().reset();
                }
            },
            /**productform的保存按钮事件*/
            "productWindow button[ref=save]": {
                click: function (_btn) {
                    //1获得form
                    var _form = _btn.ownerCt.ownerCt;
                    var _grid = Ext.getCmp("product_grid");
                    var id = _form.getForm().findField("id").getValue();
                    var status = _form.getForm().findField("status").getValue();
                    if (status == null || Ext.util.Format.trim(status) == "") {
                        Ext.Msg.alert("错误","启用状态没有选择！");
                        return;
                    }
                    var url = "batchSchedule/addSchedule.do";
                    //2.把数据保存到数据库中去
                    _form.submit({
                        clientValidation: false,
                        waitMsg: '正在进行处理,请稍后...',
                        url: url,
                        method: 'POST',
                        success: function (form, action) {
                            var resProductObj = Ext.decode(action.response.responseText);
                            if (resProductObj.success) {
                                //3.把这条数据加到grid中
                                var store = _grid.getStore();
                                store.load({
                                    start: store.start,
                                    limit: store.limit
                                });
                                Ext.Msg.alert("提示", resProductObj.msg);
                                Ext.getCmp("productWindow").close();
                                _grid.show();
                            } else {
                                Ext.Msg.alert("提示", resProductObj.msg);
                            }
                        },
                        failure: function (form, action) {
                            Ext.Msg.alert("错误", "表单数据处理有误，无法请求到后台!");
                        }
                    });

                }
            }


        });
    },
    views: ["core.product.view.ProductLayout"
        , "core.product.view.ProductGrid"
        , "core.product.view.ProductWindow"
        // , "core.product.view.ProductForm"
    ],
    stores: ["core.product.store.ProductStore"],
    models: ["core.product.model.ProductModel"]
});